//获取应用实例
const app = getApp()
let ServiceUrl = app.globalData.ServiceUrl;
Page({
  data: {
  //  isShow: false,
    user_id: '',
    peopleList: [],
    selectPeople:'',
    selectPeopleId:'',
    typeid: ''  //服务类型id
  },
  //删除关联人
  delete(event) {
    console.log(event)
    let that = this
    const { position, instance } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        wx.showModal({
          title: "提示",
          content: "确定删除吗？",
          showCancel:true,
          confirmColor: 'skyblue',
          success:function(re){
            console.log(event.currentTarget.id)
            if(re.confirm){
              wx.request({
                url: ServiceUrl + 'peopleDel',
                data: {oldman_id: event.currentTarget.id},
                method: 'post',
                dataType:'json',
                success: function(res){
                  console.log(res)
                  if(res.data.state == 200){
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'none'
                    })
                    that.request(that.data.user_id);//刷新页面
                  }else{
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'none'
                    })
                  }
                }
              })
            } 
          }
        })
        break;
    }
  },
  //选中对象
  selectPeople:function(e){
    console.log(e)
    let that = this;
    wx.request({
      url: ServiceUrl + 'getOldmanName',
      data: { oldman_id: e.currentTarget.id},
      success:function(res){
        console.log(res)
        if(res.data.state == 200){
          that.setData({
            selectPeopleId: e.currentTarget.id,
            selectPeople: res.data.data.oldman_name
          })
          wx.navigateBack({
            delta: 1,
          })
        }
      },
      error: function(){
        console.log('获取老人姓名失败')
      }
    })
  },
  //搜索对象
  onSearch: function(e){
    let that = this;
    console.log(e.detail)
    wx.request({
      url: ServiceUrl + 'getPeople',
      data: {
        oldman_name: e.detail
      },
      success: function(res){
        console.log(res)
        that.setData({ peopleList: res.data.people});
      },
      fail: function(){
        console.log('发送请求失败')
      }
    })
  },
  onCancel: function(){
    this.request(this.data.user_id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this;
    that.setData({
      typeid: options.typeid,
      user_id: options.user_id
    })
  },
  //获取就诊对象列表
  request: function(user_id){
    let that = this
    that.setData({peopleList: ''})
    wx.request({
      url: ServiceUrl + 'getPeopleList',
      data: { user_id: user_id },
      success (res){
        console.log(res);
        if(res.data.state == 200){
          that.setData({ 
            peopleList: res.data.people
          });
        }else if(res.data.state == 400){
          that.setData({
            peopleList:''
          })
        }
      },
    })
  },
  peopleAdd:function(){
    wx.navigateTo({
      url: '../information/peopleAdd?user_id=' + this.data.user_id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user_id = this.data.user_id
    this.request(user_id);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //关闭当前页面的时候，将值传递到上一个页面
    let that = this;
    //获取页面栈
    let pages = getCurrentPages();
    //获取所需页面
    let prevPage = pages[pages.length -2];//上一页
    prevPage.setData({
      selectPeople: that.data.selectPeople,//你需要传过去的数据
      selectPeopleId: that.data.selectPeopleId,
      typeid: that.data.typeid
    });
    prevPage.onLoad({user_id: that.data.user_id, typeid: that.data.typeid})
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let user_id = this.data.user_id
    this.request(user_id);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})